﻿@model IEnumerable<ViewAdminUserDept>

@{
    ViewData["Title"] = "岗位管理";
}

<div class="content-top-nav">
    <h3 style="color:#444"><i class="layui-icon layui-icon-home"></i>@ViewData["Title"] </h3>
    <hr>
</div>
<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <script type="text/html" id="toolbarAE">
                <div class="layui-btn-container">
                    @if (await Authorizer.AuthView(PermissionKeys.UserPost_AddOrEdit))
                    {
                                <button class="layui-btn layui-btn-sm data-add-btn"><i class="layui-icon">&#xe642;</i>新增</button>
                    }
                </div>
            </script>

            <table class="layui-hide" id="currentTableId" lay-data="{id: 'idReload'}" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
                 <div class="layui-btn-group">
                    @if (await Authorizer.AuthView(PermissionKeys.UserPost_AddOrEdit))
                    {
                        <a class="layui-btn layui-btn-sm data-count-edit" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>
                    }
                    @if (await Authorizer.AuthView(PermissionKeys.UserPost_Delete))
                    {
                            <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</a>
                    }
                </div>
            </script>
        </div>
    </div>
</div>

<script>
    layui.define(function () {
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form;

        table.render({
            elem: '#currentTableId',
            //url: '/resapi/table.json',
            url: '/Admin/UserPost/Index?json=1',
            toolbar: '#toolbarAE',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                , groups: 1 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            cols: [[
                { field: 'id', width: 70, title: 'ID', sort: true },
                { field: 'postName', width: 200, title: '岗位名称' },
                { field: 'postCode', width: 160, title: '代码' },
                { field: 'sort', width: 160, title: '排序', edit: 'text' },
                { field: 'remark', title: '备注' },
                { title: '操作', templet: '#currentTableBar', width: 260, fixed: "right", align: "center" }
            ]],
            id: 'tableReload',
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        });


        $(".data-add-btn").on("click", function () {
            loadMain('/Admin/UserPost/AddOrEdit');
            return false;
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                loadMain('/Admin/UserPost/AddOrEdit?id=' + data.id);
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除 名为' + data.postName + '的岗位吗？', {
                    btn: ['确定', '取消']
                }, function (index) {
                    $.post('/Admin/UserPost/Delete?id=' + data.id, function (res) {
                        if (res.status == 1) {
                            layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                                obj.del();
                                layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                        }
                    });
                });
            }
        });

        //监听单元格编辑
        table.on('edit(currentTableFilter)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            $.post('/Admin/UserPost/EditPost?dyEdit=1&id=' + data.id, { Sort: value }, function (res) {
                if (res.status == 1) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        table.reloadData('tableReload');
                    });
                } else {
                    layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                }
            });
        });

    });

</script>
